

source('code/analysis/load.R')


data = read_dta('data/working/deport-sender.dta') %>%
  mutate(exp = 'exp3') %>% 
  bind_rows(read_dta('data/working/defund-sender.dta') %>% mutate(exp = 'exp1', replication=0)) %>% 
  bind_rows(read_dta('data/working/defund-sender-replication.dta') %>% mutate(exp = 'exp1',ind=0, replication=1)) %>%
  bind_rows(read_dta('data/working/defund-receiver.dta') %>% mutate(exp = 'exp2', deny = !bonus)) %>% 
  bind_rows(read_dta('data/working/deport-receiver.dta') %>% mutate(exp = 'exp4', deny = !bonus)) %>% 
  data_finalize()


main_result_partisan = function(data) {
  
  
  
  ## Panel A: Exp.1 + Exp.1R
  title = 'Heterogeneity by partisan affiliation (Experiments 1--4)'
  depvar = '\\textit{Scheduled Tweet}'
  label = 'partisan-heterogeneity'
  
  model1 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education+replication, data=data %>% filter(exp=='exp1'))
  model2 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education+replication, data=data %>% filter(exp=='exp1' & ind == 0))
  model3 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education+replication, data=data %>% filter(exp=='exp1' & ind == 1))
  
  mean =   unlist(formatC(c(mean(data$posted[data$excuse==0 & data$exp=='exp1'], na.rm = T),
                                  mean(data$posted[data$excuse==0 & data$exp=='exp1' & data$ind==0], na.rm = T),
                                  mean(data$posted[data$excuse==0 & data$exp=='exp1' & data$ind==1], na.rm = T)
                                  ), digits=3))

  out = stargazer(list(model1, model2, model3), 
                  omit='age|race|hisp|male|rep|education|Constant|partisan|replication',
                  covariate.labels = c('Cover'),
                  keep.stat = c('rsq','adj.rsq', 'n'), dep.var.labels = depvar,
                  column.labels = c('All', 'Partisan', 'Independent'),
                  title = title, label = str_interp('t:${label}'),
                  add.lines = list(c('No Cover mean', mean)))
  
  out1 = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[["partisan-heterogeneity"]])

  ## Panel B: Exp.2
  # Subpanel A
  title = 'Experiment 2'
  depvar = "\\textit{Belief partner donated}"
  label = 'defund-receiver-partisan1'
  model1 = lm(believe_donated~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp2'))
  model2 = lm(believe_donated~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp2' & ind==0))
  model3 = lm(believe_donated~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp2' & ind==1))
  
  mean =   unlist(formatC(c(mean(data$believe_donated[data$excuse==0 & data$exp=='exp2'], na.rm = T),
                                  mean(data$believe_donated[data$excuse==0 & data$exp=='exp2' & data$ind==0], na.rm = T),
                                  mean(data$believe_donated[data$excuse==0 & data$exp=='exp2' & data$ind==1], na.rm = T)
                                  ), digits=3))
  
  out = stargazer(list(model1, model2, model3), 
                  omit='age|race|hisp|male|rep|education|Constant|partisan',
                  covariate.labels = c('Cover'),
                  keep.stat = c('rsq','adj.rsq', 'n'), dep.var.labels = depvar,
                  column.labels = c('All', 'Partisan', 'Independent'),
                  title = title, label = str_interp('t:${label}'),
                  add.lines = list(c('No Cover mean', mean)))
  
  
  out2_1 = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[["partisan-heterogeneity"]])

  # Subpanel B
  title = 'Experiment 2'
  depvar = "\\textit{Denied bonus to partner}"
  label = 'defund-receiver-partisan2'
  model1 = lm(deny~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp2'))
  model2 = lm(deny~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp2' & ind==0))
  model3 = lm(deny~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp2' & ind==1))
  
  mean =   unlist(formatC(c(mean(data$deny[data$excuse==0 & data$exp=='exp2'], na.rm = T),
                                  mean(data$deny[data$excuse==0 & data$exp=='exp2' & data$ind==0], na.rm = T),
                                  mean(data$deny[data$excuse==0 & data$exp=='exp2' & data$ind==1], na.rm = T)
  ), digits=3))
  
  out = stargazer(list(model1, model2, model3), 
                  omit='age|race|hisp|male|rep|education|Constant|partisan',
                  covariate.labels = c('Cover'),
                  keep.stat = c('rsq','adj.rsq', 'n'), dep.var.labels = depvar,
                  column.labels = c('All', 'Partisan', 'Independent'),
                  title = title, label = str_interp('t:${label}'),
                  add.lines = list(c('No Cover mean', mean)))

  out2_2 = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[["partisan-heterogeneity"]])
  
  ## Panel C: Exp.3
  title = 'Experiment 3'
  depvar = "\\textit{Scheduled Tweet}"
  label = 'deport-sender-partisan'
  model1 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp3'))
  model2 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp3' & ind==0))
  model3 = lm(posted~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp3' & ind==1))
  
  mean =   unlist(formatC(c(mean(data$posted[data$excuse==0 & data$exp=='exp3'], na.rm = T),
                                  mean(data$posted[data$excuse==0 & data$exp=='exp3' & data$ind==0], na.rm = T),
                                  mean(data$posted[data$excuse==0 & data$exp=='exp3' & data$ind==1], na.rm = T)
  ), digits=3))
  
  out = stargazer(list(model1, model2, model3), 
                  omit='age|race|hisp|male|rep|education|Constant|partisan',
                  covariate.labels = c('Cover'),
                  keep.stat = c('rsq','adj.rsq', 'n'), dep.var.labels = depvar,
                  column.labels = c('All', 'Partisan', 'Independent'),
                  title = title, label = str_interp('t:${label}'),
                  add.lines = list(c('No Cover mean', mean)))
  
  means = str_interp('No Cover mean & ${mean}  \\\\')
  
  out3 = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[["partisan-heterogeneity"]])

  ## Panel D: Exp.4
  # Subpanel A
  title = 'Experiment 4'
  depvar = "\\textit{Belief partner donated}"
  label = 'deport-receiver-partisan1'
  model1 = lm(believe_donated~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp4'))
  model2 = lm(believe_donated~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp4' & ind==0))
  model3 = lm(believe_donated~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp4' & ind==1))
  
  mean =   unlist(formatC(c(mean(data$believe_donated[data$excuse==0 & data$exp=='exp4'], na.rm = T),
                                  mean(data$believe_donated[data$excuse==0 & data$exp=='exp4' & data$ind==0], na.rm = T),
                                  mean(data$believe_donated[data$excuse==0 & data$exp=='exp4' & data$ind==1], na.rm = T)
  ), digits=3))
  
  out = stargazer(list(model1, model2, model3), 
                  omit='age|race|hisp|male|rep|education|Constant|partisan',
                  covariate.labels = c('Cover'),
                  keep.stat = c('rsq','adj.rsq', 'n'), dep.var.labels = depvar,
                  column.labels = c('All', 'Partisan', 'Independent'),
                  title = title, label = str_interp('t:${label}'),
                  add.lines = list(c('No Cover mean', mean)))
  
  out4_1 = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[["partisan-heterogeneity"]])
  
  # Subpanel B
  title = 'Experiment 4'
  depvar = "\\textit{Denied bonus to partner}"
  label = 'defund-receiver-partisan2'
  model1 = lm(deny~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp4'))
  model2 = lm(deny~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp4' & ind==0))
  model3 = lm(deny~excuse+age+I(age^2)+race+hisp+male+education, data=data %>% filter(exp=='exp4' & ind==1))
  
  mean =   unlist(formatC(c(mean(data$deny[data$excuse==0 & data$exp=='exp4'], na.rm = T),
                                  mean(data$deny[data$excuse==0 & data$exp=='exp4' & data$ind==0], na.rm = T),
                                  mean(data$deny[data$excuse==0 & data$exp=='exp4' & data$ind==1], na.rm = T)
  ), digits=3))
  
  out = stargazer(list(model1, model2, model3), 
                  omit='age|race|hisp|male|rep|education|Constant|partisan',
                  covariate.labels = c('Cover'),
                  keep.stat = c('rsq','adj.rsq', 'n'), dep.var.labels = depvar,
                  column.labels = c('All', 'Partisan', 'Independent'),
                  title = title, label = str_interp('t:${label}'),
                  add.lines = list(c('No Cover mean', mean)))
  

  out4_2 = star_notes_tex(out, note.type = 'threeparttable', note = tablenotes[["partisan-heterogeneity"]])


  out1[7] = paste0('\\scalebox{0.9}{', out1[7])
  out4_2[28] = paste0(out4_2[28], '}')
  out = c(
    out1[1:9],
    out1[14:16], # three columns
    '\\multicolumn{4}{l}{\\textbf{Panel A:} Experiments 1 and 1R (pooled)} \\\\ \\midrule',
    out1[12:13], # dep label
    out1[17:19],
    out1[21],
    '\\midrule',
    '\\multicolumn{4}{l}{\\textbf{Panel B:} Experiment 2} \\\\ \\midrule',
    out2_1[12:13], # dep label 
    out2_1[17:19],
    out2_1[21],
    '\\midrule',
    out2_2[12:13], # dep label 
    out2_2[17:19],
    out2_2[21],
    '\\midrule',
    '\\multicolumn{4}{l}{\\textbf{Panel C:} Experiment 3} \\\\ \\midrule',
    out3[12:13], # dep label
    out3[17:19],
    out3[21],
    '\\midrule',
    '\\multicolumn{4}{l}{\\textbf{Panel D:} Experiment 4} \\\\ \\midrule',
    out4_1[12:13], # dep label
    out4_1[17:19],
    out4_1[21],
    '\\midrule',
    out4_2[12:13], # dep label
    out4_2[17:19],
    out4_2[21],
    out4_2[25: length(out4_2)])
  
  writeLines(out, con = str_interp('output/tables/partisan-heterogeneity.tex'))
  
}


main_result_partisan(data)



